package edu.seminario

import grails.converters.JSON

import org.codehaus.groovy.grails.web.json.JSONObject

class LoginController {

	LoginService loginService

    def index = { }

	/**
	* Viene de esta forma
	* {username: 'pepito', password: 'hola' }
	*
	* Criterios:
	* Falta poner los criterios
	*
	*
	* Devlvemos de esta forma
	*
	* {success: true / false, user_admin_id: 0 }
	*
	*/
	def auth = {
//		String json = "{username: 'admin', password: 'pepito'}"
//		JSONObject jsonObject = JSON.parse(json)
		
 		JSONObject jsonObject = JSON.parse(params.json)
 
		def username = jsonObject.username
		def password = jsonObject.password
		
		UsuarioAdmin usuarioAdmin = loginService.auth(username, password)
		JSONObject answerJson = new JSONObject()
		answerJson.put('success', usuarioAdmin != null)
		answerJson.put('user_admin_id',  usuarioAdmin != null ? usuarioAdmin.id : 0)
		jsonOutput(answerJson)
	}

	def jsonOutput(Object json) {
		response.setHeader('Content-disposition', 'filename="json"')
		response.contentType = "text/json"
		render json as JSON
	}
}
